import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.util.HashMap;
import java.util.HashSet;


public class DuplicationRemove {

	public static void main(String [] args)
	{
		String dir=args[0];
		String ddir=args[1];
		File file=new File(dir);
		HashSet<String> hm=new HashSet<String>();
		
		if(file.isDirectory())
		{
			File[] filelist=file.listFiles();
			for(File tempfile:filelist)
			{
				String tempfilename=tempfile.getName();
				int underscoreindex=tempfilename.indexOf("_");
				String substr=tempfilename.substring(0, underscoreindex);
				
				if(hm.contains(substr))
				{
					//if it already exists
					continue;
				}
				else
				{
					hm.add(substr);
					//copy the file to other place
					
					try {
						String dtFile=ddir+substr;

						File f2 = new File(dtFile);
						if(!f2.exists())
						{
							f2.createNewFile();
						}
						InputStream in;
						in = new FileInputStream(tempfile);
						//For Append the file.
						//					      OutputStream out = new FileOutputStream(f2,true);

						//For Overwrite the file.
						OutputStream out = new FileOutputStream(f2);

						byte[] buf = new byte[1024];
						int len;
						while ((len = in.read(buf)) > 0){
							out.write(buf, 0, len);
						}
						in.close();
						out.close();
						System.out.println("File copied.");
					} catch (FileNotFoundException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					} catch (IOException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}

				     

				}
			}
		}
		
	}
}
